/*
 * SaveForwards.java
 *
 * Version information
 *
 * Apr 11, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.ilink.webapp.struts.action;

import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.ilink.service.ILinkService;
import net.amsoft.iservice.ilink.util.ILinkConstants;
import net.amsoft.iservice.ilink.webapp.dataObject.LoginData;
import net.amsoft.iservice.ilink.webapp.struts.form.ForwardsForm;
import net.amsoft.iservice.util.exception.IServiceException;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.openxri.XRI;
import org.openxri.XRIParseException;

/**
 * The SaveForwards action class is used to save I-Links of the i-name.
 * 
 * @author mohit
 */
public class SaveForwards extends BaseAction {
	Logger oLogger = Logger.getLogger(SaveForwards.class);

	public ActionForward process(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse) {
		oLogger.info("process() : entry");
		LoginData oLoginData = (LoginData) oRequest.getSession(false)
				.getAttribute(ILinkConstants.SESSION_LOGIN_OBJ);
		ResourceBundle bundle = ResourceBundle.getBundle("ilinkResources",
				oRequest.getLocale());
		String message = null;

		if (oLoginData.isLoginStatus() == false) {
			oLogger.info("process() : Login Status: false");
			message = bundle.getString(ILinkConstants.ALLERT_SESSION_EXPIRE);
			oRequest.setAttribute(ILinkConstants.REQ_ATTRIBUTE_STATUS, Integer
					.valueOf(-1));
			oRequest
					.setAttribute(ILinkConstants.REQ_ATTRIBUTE_MESSAGE, message);
			return oMapping.getInputForward();
		}

		ForwardsForm oForwardsForm = (ForwardsForm) oForm;

		switch (oForwardsForm.getOpType()) {
		case 1:
			if (saveForward(oForwardsForm, oLoginData.getLoggedInSynonym())) {
				message = ILinkConstants.MSG_SAVE_SUCCESS;
				oRequest.setAttribute(ILinkConstants.REQ_ATTRIBUTE_STATUS, "1");
			} else {
				message = ILinkConstants.MSG_SAVE_FAIL;
				oRequest.setAttribute(ILinkConstants.REQ_ATTRIBUTE_STATUS, "0");
			}
			oRequest.setAttribute(ILinkConstants.REQ_ATTRIBUTE_OPTYPE, "1");
			break;

		case 2:
			if (updateForward(oForwardsForm)) {
				message = ILinkConstants.MSG_UPDATE_SUCCESS;
				oRequest.setAttribute(ILinkConstants.REQ_ATTRIBUTE_STATUS, "1");
			} else {
				message = ILinkConstants.MSG_UPDATE_FAIL;
				oRequest.setAttribute(ILinkConstants.REQ_ATTRIBUTE_STATUS, "0");
			}
			oRequest.setAttribute(ILinkConstants.REQ_ATTRIBUTE_OPTYPE, "2");
			break;

		case 3:
			if (deleteForward(oForwardsForm)) {
				message = ILinkConstants.MSG_DELETE_SUCCESS;
				oRequest.setAttribute(ILinkConstants.REQ_ATTRIBUTE_STATUS, "1");
			} else {
				message = ILinkConstants.MSG_DELETE_FAIL;
				oRequest.setAttribute(ILinkConstants.REQ_ATTRIBUTE_STATUS, "0");
			}
			oRequest.setAttribute(ILinkConstants.REQ_ATTRIBUTE_OPTYPE, "3");
			break;

		default:
			break;
		}
		message = bundle.getString(message);
		oRequest.setAttribute(ILinkConstants.REQ_ATTRIBUTE_MESSAGE, message);
		return oMapping.getInputForward();
	}

	/**
	 * It creates new I-Links for i-name
	 * 
	 * @param oForwardsForm
	 *            Form containing the forwards/i-links data
	 * @param sLoggedInSynonym
	 *            Name of logged in synonym
	 * @return true if created ilink successfully saved , false otherwise
	 */
	private boolean saveForward(ForwardsForm oForwardsForm,
			String sLoggedInSynonym) {
		oLogger.info("saveForward() : entry");
		try {
			String sXRI = "xri://" + sLoggedInSynonym + "/"
					+ oForwardsForm.getTag();
			XRI oXRI = new XRI(sXRI);
			if (oXRI.isAbsolute()) {
				ILinkService.createRedirect(oForwardsForm.getIname(),
						oForwardsForm.getTag(), oForwardsForm.getWebResource(),
						oForwardsForm.getIsPublic(), sLoggedInSynonym);
				return true;
			} else {
				return false;
			}
		} catch (XRIParseException eXRIParseExp) {
			oLogger.warn("saveForward() : XRIParseException: "
					+ eXRIParseExp.getMessage() + ",  Return=false");
			return false;
		} catch (IServiceException e) {
			oLogger.warn("saveForward() : Exception: " + e.getMessage()
					+ ",  Return=false");
			return false;
		}
	}

	/**
	 * Updates ilink with new values
	 * 
	 * @param oForwardsForm
	 *            Form containing the forwards/i-links data
	 * @return true if selected ilink is deleted, false otherwise
	 */
	private boolean updateForward(ForwardsForm oForwardsForm) {
		oLogger.info("updateForward() : entry");
		try {
			ILinkService.updateRedirect(oForwardsForm.getIname(), oForwardsForm
					.getTag(), oForwardsForm.getWebResource(), oForwardsForm
					.getIsPublic(), oForwardsForm.getOldIname(), oForwardsForm
					.getOldTag());
			return true;
		} catch (IServiceException ise) {
			oLogger.warn("updateForward() : Exception: " + ise.getMessage()
					+ ", Return=false");
			return false;
		}
	}

	/**
	 * Deletes ilink having combination of iname and tag
	 * 
	 * @param oForwardsForm
	 *            Form containing the forwards/ilinks data
	 * @return true if selected ilink is deleted , false otherwise
	 */
	private boolean deleteForward(ForwardsForm oForwardsForm) {
		oLogger.info("deleteForward() : entry");
		try {
			ILinkService.deleteRedirects(oForwardsForm.getIname(),
					oForwardsForm.getTag());
			return true;
		} catch (IServiceException ise) {
			oLogger.warn("deleteForward() : Exception: " + ise.getMessage()
					+ ", Return=false");
			return false;
		}
	}
}